package com.ruoyi.common.utils;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.domain.SearchCallResponse;
import com.ruoyi.common.domain.SendCallResponse;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.common.utils.sign.Md5Utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.net.URLEncoder;
@Slf4j
@Component
public class TeamCallUtils {

    @Value("${call.url}")
    private String callUrl;

    @Value("${call.userName}")
    private String userName;

    @Value("${call.password}")
    private String password;

    @Value("${call.TCL_Phone}")
    private String tclPhone;
    @Value("${call.TCL_Priority}")
    private String tclPriority;
    @Value("${call.TCL_Type}")
    private String tclType;
    @Value("${call.TCDL_CallCountMax}")
    private String tcdlCallCountMax;

    /**
     * 发起外呼接口
     * @param tclName 外呼名称
     * @param content 维护内容
     * @param tcdlArray 外呼名单
     */

    public SendCallResponse sendCall(String tclName, String content, String tcdlArray){
        StringBuilder sb = new StringBuilder();
        sb.append("?action=SendCall")
                .append("&Username=").append(URLEncoder.encode(userName))
                .append("&Password=").append(Md5Utils.hash(password))
                .append("&TCL_Name=").append(URLEncoder.encode(tclName))
                .append("&TCL_Priority=").append(tclPriority)
                .append("&TCL_Type=").append(tclType)
                .append("&TCL_Phone=").append(tclPhone)
                .append("&TCL_Text=").append(URLEncoder.encode(content))
                .append("&TCDL_CallCountMax=").append(tcdlCallCountMax)
                .append("&TCL_Remarks=").append(URLEncoder.encode("外呼"))
                .append("&jsonArray=").append(URLEncoder.encode(tcdlArray));
        String result = HttpUtils.httpGet(callUrl + sb.toString());
        if(StringUtils.isNotBlank(result)){
            JSONObject jsonObject = JSONObject.parseObject(result);
            if(jsonObject.getInteger("Success") == 1){
                log.info(jsonObject.getString("Msg"));
                return JSON.parseObject(result, SendCallResponse.class);
            }else{
                log.info("外呼失败，失败原因：{}", jsonObject.getString("Msg"));
            }
        }
        return null;
    }

    /**
     * 外呼查询接口
     * @param tclId
     * @return
     */
    public SearchCallResponse searchCall(Integer tclId){
        if(tclId == null)return null;

        StringBuilder sb = new StringBuilder();
        sb.append("?action=SearchCall").append("&TCL_ID=").append(tclId);
        String result = HttpUtils.httpGet(callUrl + sb.toString());
        //请求结果
        if(StringUtils.isNotBlank(result)){
            JSONObject jsonObject = JSONObject.parseObject(result);
            if(jsonObject.getInteger("Success") == 1){
                log.info(jsonObject.getString("Msg"));
                return JSON.parseObject(result, SearchCallResponse.class);
            }else{
                log.info("外呼失败，失败原因：{}", jsonObject.getString("Msg"));
            }
        }
        return null;
    }

    public static void main(String[] args) {
        TeamCallUtils teamCallUtils = new TeamCallUtils();
        JSONArray jsonArray1 = new JSONArray();
        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("TCDL_PhoneNum", "13636524118");
        jsonObject1.put("TCDL_CustomerName", "孙行者");
        jsonArray1.add(jsonObject1);
        SendCallResponse sendCallResponse = teamCallUtils.sendCall("测试外呼", "测试外呼", jsonArray1.toJSONString());
        teamCallUtils.searchCall(sendCallResponse.getTCL_ID());
    }

}
